package com.qqt.csr.im.entity.mq;

import com.qqt.csr.common.idempotent.RequestKeyParam;
import com.qqt.csr.common.utils.JsonUtil;
import com.qqt.csr.common.utils.SnowflakeUtil;
import com.qqt.csr.im.entity.msg.Message;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;

/**
 * @Author: yang hao
 * @Date: 2024/10/15 15:43
 * @Description: 用于mq传输的消息实体
 **/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "用于mq传输的消息实体")
public class MqMessage implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    @Schema(description = "服务id")
    private Long serviceId;

    @Schema(description = "设备key")
    private String deviceKey;

    @Schema(description = "消息唯一键")
    @RequestKeyParam
    private String uniqueKey;

    @Schema(description = "消息")
    private Message message;

    /**
     * 推送事件的对象
     */
    private Object eventData;

    @Override
    public String toString() {
        return JsonUtil.toJson(this);
    }

    public static MqMessage of(Long serviceId, String deviceKey, Message message, Object eventData) {
        return MqMessage.builder()
                .serviceId(serviceId)
                .deviceKey(deviceKey)
                .uniqueKey(SnowflakeUtil.nextIdStr())
                .message(message)
                .eventData(eventData)
                .build();
    }

}
